package me.zhengjie.modules.businessRevoke.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.account.mapper.AccountAc50Mapper;
import me.zhengjie.modules.basic.domain.BasicAc02;
import me.zhengjie.modules.basic.mapper.BasicAc02Mapper;
import me.zhengjie.modules.businessRevoke.service.BusinessRevokeService;
import me.zhengjie.modules.fileupload.domain.ElectronicMaterialFileDetailVO;
import me.zhengjie.modules.fileupload.domain.ElectronicMaterialFileVO;
import me.zhengjie.modules.fileupload.mapper.ElectronicMaterialMapper;
import me.zhengjie.modules.fileupload.service.ElectronicMaterialService;
import me.zhengjie.modules.fileupload.utils.FileUtils;
import me.zhengjie.modules.finance.mapper.FinanceAd01Mapper;
import me.zhengjie.modules.logs.domain.BusinessLog;
import me.zhengjie.modules.logs.mapper.BusinessLogMapper;
import me.zhengjie.modules.logs.service.BusinessLogService;
import me.zhengjie.modules.transfer.service.TransferOutService;
import me.zhengjie.tool.DateUtil;
import me.zhengjie.tool.ListUtil;
import me.zhengjie.tool.MyMap;
import me.zhengjie.tool.ValidateUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import me.zhengjie.vo.Constant;
import me.zhengjie.vo.YHConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

@Service
@Slf4j
@Transactional
@RequiredArgsConstructor
public class BusinessRevokeServiceImpl implements BusinessRevokeService {

    @Resource
    private BusinessLogMapper businessLogMapper;
    @Resource
    private AccountAc50Mapper accountAc50Mapper;
    @Resource
    private BasicAc02Mapper basicAc02Mapper;
    @Resource
    private FinanceAd01Mapper financeAd01Mapper;

    private final TransferOutService transferOutService;


    @Override
    public PageResult<BusinessLog> queryList(Map param, Page<Object> page) {
        List<String> aaa121s = new ArrayList<>();
        aaa121s.add(YHConstants.AAA121_TF);
        aaa121s.add(YHConstants.AAA121_040101);
        aaa121s.add(YHConstants.AAA121_040102);
        param.put("aaa121s", aaa121s);
        return PageUtil.toPage(businessLogMapper.getList(param, page));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map toRevoke(Map param) {
        String aaa121 = param.get("aaa121").toString();

        param.put("aae016",Constant.AAE016_1_SHTG);
        List<BusinessLog> businessLogList = businessLogMapper.getList(param);

        // 1.处理退费的撤销
        if (YHConstants.AAA121_TF.equals(aaa121)) {
            if (ValidateUtil.isEmpty(businessLogList)) {
                throw new RuntimeException("没有查询到可撤销的日志记录!");
            }
            revokeTf(param);
        }
        // 1.处理退费的撤销
        if (YHConstants.AAA121_040101.equals(aaa121)|| YHConstants.AAA121_040102.equals(aaa121)) {
            if (ValidateUtil.isEmpty(businessLogList)) {
                throw new RuntimeException("没有查询到可撤销的日志记录!");
            }
            MyMap myMap = new MyMap<>();
            myMap.putAll(param);
            transferOutService.transferOutRevokeSave(myMap);

            // 更新日志为撤销
            Map updateMap = new HashMap();
            updateMap.put("aaz002",param.get("aaz002").toString());
            updateMap.put("aae016",(Constant.AAE016_4_YHT));
            updateMap.put("aae017",param.get("reason"));
            updateMap.put("aae018", SecurityUtils.getCurrentUsername());
            updateMap.put("aae019",DateUtil.getStringCurDateTime());
            businessLogMapper.update(updateMap);

        }

        return null;
    }

    /**退费撤销
     * @Author ly
     * @Description
     * @Date 9:36 2025/7/15
     **/
    private void revokeTf(Map param) {

        // 撤销账户
        List<Map> ac50z1List = accountAc50Mapper.getAc50z1(param);
        if (!ValidateUtil.isEmpty(ac50z1List)) {
            for (Map item:ac50z1List
                 ) {
                List<Map> updateList = new ArrayList<>();
                updateList.add(item);
                accountAc50Mapper.updateBatchAc50(updateList);
            }
        } else {
            throw new RuntimeException("没有查询到可撤销的业务记录");
        }

        // 撤销参保
        Map query = new HashMap();
        query.put("aae140",Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        query.put("aac008",Constant.AAC008_4_ZZCB);
        query.put("aaz159s", ListUtil.listMap2ListString(ac50z1List,"aaz159"));
        List<BasicAc02> ac02List = basicAc02Mapper.ggetAc02List(query);
        if (!ValidateUtil.isEmpty(ac02List)) {
            for (BasicAc02 item:
                    ac02List) {
                BasicAc02 queryParam = new BasicAc02();
                queryParam.setAaz159(item.getAaz159());
                BasicAc02 basicAc02 = basicAc02Mapper.selectByAaz159(queryParam);
                basicAc02.setAaz159(item.getAaz159());
                basicAc02.setAac001(item.getAac001());
                basicAc02.setAae042(null);
                basicAc02.setAac008(Constant.AAC008_1_ZCCB);
                basicAc02.setAac031(Constant.AAC031_2_ZTJF);
                basicAc02Mapper.updateByAaz159(basicAc02);
            }
        }

        // 撤销财务单据
        Map queryMap = new HashMap();
        queryMap.put("aaz002",param.get("aaz002").toString());
        financeAd01Mapper.deleteByMap(queryMap);

        // 更新日志为撤销
        Map updateMap = new HashMap();
        updateMap.put("aaz002",param.get("aaz002").toString());
        updateMap.put("aae016",(Constant.AAE016_4_YHT));
        updateMap.put("aae017",param.get("reason"));
        updateMap.put("aae018", SecurityUtils.getCurrentUsername());
        updateMap.put("aae019",DateUtil.getStringCurDateTime());
        businessLogMapper.update(updateMap);


    }

}
